package logic

import (
	"context"
	"strconv"
	"user_srv/model/mysql"

	"user_srv/internal/svc"
	"user_srv/user"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LoginLogic) Login(in *user.LoginRequest) (*user.LoginResponse, error) {
	// todo: add your logic here and delete this line
	u := mysql.User{}
	err := u.GetUser(in.Username)
	if err != nil {
		logx.Error("用户信息获取失败", err)
		return nil, err
	}
	if u.Password != in.Password {
		logx.Error("密码错误", err)
		return nil, err
	}
	return &user.LoginResponse{Id: strconv.Itoa(int(u.ID))}, nil
}
